package com.hzw.saas.service.product.service;

import java.util.List;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.hpc.IHpcAccountService;
import com.hzw.saas.api.hpc.IHpcAccountUserService;
import com.hzw.saas.api.hpc.IHpcItemService;
import com.hzw.saas.api.product.dto.HpcItemDto;
import com.hzw.saas.common.config.exception.DefaultSaasException;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.config.util.PageUtils;
import com.hzw.saas.common.util.IdWorker;
import com.hzw.saas.service.product.mapper.HpcItemMapper;
import com.hzw.saas.service.product.model.HpcItem;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import ma.glasnost.orika.MapperFacade;

/**
 * <p>
 * 超算中心商品表 服务实现类
 * </p>
 *
 * @author zzl
 * @since 2021-01-21
 */
@Service("hpcItemServiceImpl")
@RequiredArgsConstructor
public class HpcItemServiceImpl extends ServiceImpl<HpcItemMapper, HpcItem> implements IHpcItemService, IService<HpcItem> {

    private static final String CACHE_NAME = "item";
    private final IdWorker idWorker;
    private final MapperFacade mapperFacade;
    @Resource(name="hpcAccountUserServiceImpl")
    private IHpcAccountUserService hpcAccountUserService;
    @Resource(name="hpcAccountServiceImpl")
    private IHpcAccountService hpcAccountService;

    @Cacheable(cacheNames = {CACHE_NAME}, key = "#itemId")
    @Override
    public HpcItemDto get(String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return null;
        }
        HpcItem hpcItem = this.getById(itemId);
        return mapperFacade.map(hpcItem, HpcItemDto.class);
    }

    @Override
    public List<HpcItemDto> getAll(String hpcId) {
        if (StringUtils.isBlank(hpcId)) {
            return null;
        }
        List<HpcItem> list = this.lambdaQuery().eq(HpcItem::getHpcId, hpcId).list();
        return mapperFacade.mapAsList(list, HpcItemDto.class);
    }

    @Override
    public IPage<HpcItemDto> list(PageParam pageParam, String hpcId) {
        Page<HpcItem> page = this.page(pageParam.convert(), Wrappers.<HpcItem>lambdaQuery().eq(StringUtils.isNotBlank(hpcId), HpcItem::getHpcId, hpcId));
        return PageUtils.convert(page, HpcItemDto.class);
    }

    @CachePut(cacheNames = {CACHE_NAME}, key = "#result.itemId")
    @Override
    public HpcItemDto add(HpcItemDto hpcItemDto) {
        hpcItemDto.setItemId(String.valueOf(idWorker.nextId()));
        this.save(mapperFacade.map(hpcItemDto, HpcItem.class));
        return hpcItemDto;
    }

    @CachePut(cacheNames = {CACHE_NAME}, key = "#hpcItemDto.itemId")
    @Override
    public boolean update(HpcItemDto hpcItemDto) {
        if (StringUtils.isBlank(hpcItemDto.getItemId())) {
            throw DefaultSaasException.build("item id is blank").status(HttpStatus.BAD_REQUEST);
        }
        return this.updateById(mapperFacade.map(hpcItemDto, HpcItem.class));
    }

    @CacheEvict(cacheNames = {CACHE_NAME}, key = "#itemId")
    @Override
    public HpcItemDto delete(String itemId) {
        if (StringUtils.isBlank(itemId)) {
            throw DefaultSaasException.build("item id is blank").status(HttpStatus.BAD_REQUEST);
        }
        HpcItem hpcItem = this.getById(itemId);
        boolean delete = this.removeById(itemId);
        if (!delete) {
            throw DefaultSaasException.build("delete error");
        }
        return mapperFacade.map(hpcItem, HpcItemDto.class);
    }

    @Override
    public boolean verify(String itemId, String hpcAccountId) {
//        if (StringUtils.isBlank(itemId) || StringUtils.isBlank(hpcAccountId)) {
            return false;
//        }
//        HpcItem hpcItem = this.getById(itemId);
//        if (hpcItem == null) {
//            return false;
//        }
//        HpcAccountRecordBo hpcAccountDto = hpcAccountUserService.get(hpcAccountId);
//        if (hpcAccountDto == null) {
//            return false;
//        }
//        return Objects.equals(hpcItem.getHpcId(), hpcAccountDto.getHpcAccountPoolBo().getHpcId());
    }

    private String cacheKey(String key) {
        return CACHE_NAME + "::" + key;
    }
}
